package com.daidai.controller;

import com.daidai.domain.Common;
import com.daidai.domain.Employee;
import com.daidai.service.employee.impl.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@CrossOrigin
@RequestMapping("employees")
public class EmployeeController {
    @Autowired
    EmployeeServiceImpl employeeService;

    @PostMapping("/login")
    public Common login(@RequestBody Employee employee, HttpSession session) {
        try {
            return employeeService.login(employee, session);
        } catch (Exception e) {
            return Common.error(500, "登录失败");
        }
    }

    @PostMapping("isManger/{token}")
    public Boolean isManger(@PathVariable("token") String token, HttpSession session) {
        Common ems = employeeService.isLoginByToken(token, session);
        if (ems.getStatus() == 200) {
            Employee employee = (Employee) ems.getData();
            if (employee.getIsManager() == 1) {
                return true;
            }
        }
        return false;
    }

    @PostMapping("getEmsByToken/{token}")
    public Common getEmsByToken(@PathVariable("token") String token, HttpSession session) {
        return employeeService.isLoginByToken(token, session);
    }

    @GetMapping("/logout/{token}")
    public Common logout(@PathVariable String token, HttpSession session) {
        return employeeService.logout(token, session);
    }

    // 管理员调用
    @GetMapping("/getAll")
    public Common getAllEmployee() {
        try {
            return Common.success("查询成功", employeeService.getAllEms());
        } catch (Exception e) {
            return Common.error(500, "查询失败");
        }
    }

    @GetMapping("/getEmsById/{id}")
    public Common getEmsById(@PathVariable int id) {
        return employeeService.getEmsById(id);
    }

    @PostMapping("updateEms")
    public Common updateEms(@RequestBody Employee employee) {
        Boolean updateEms = employeeService.updateEms(employee);
        try {
            if (updateEms) {
                return Common.success("修改成功", updateEms);
            }
        } catch (Exception e) {
            return Common.error(500, "修改出错");
        }
        return Common.error(500, "修改失败");
    }

    @GetMapping("/getWorking")
    public Common getWorkingEmS() {
        try {
            return Common.success("查询成功", employeeService.getWorkingEmS());
        } catch (Exception e) {
            return Common.error(500, "查询失败");
        }
    }

    @GetMapping("/getNoWork")
    public Common getNOWorkEmS() {
        try {
            return Common.success("查询成功", employeeService.getNoWorkEmS());
        } catch (Exception e) {
            return Common.error(500, "查询失败");
        }
    }

    @PostMapping("/addEms")
    public Common addEms(@RequestBody Employee employee) {
        return employeeService.addEms(employee);
    }

    @DeleteMapping("/delEms/{eid}")
    public Common delEms(@PathVariable int eid) {
        return employeeService.delEms(eid);
    }

    @GetMapping("/forceLeave/{eid}")
    public Common forceLeave(@PathVariable int eid, HttpSession session) {
        Boolean aBoolean = employeeService.forceLeave(eid, session);
        if (aBoolean) {
            return Common.success("该员工下线成功");
        }
        return Common.error(500, "退出失败");
    }

    @PostMapping("/forceLogout")
    public Common forceLogout(@RequestBody Employee employee, HttpSession session) {
        return employeeService.forceLogout(employee, session);
    }
}

